import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:short_drama/pages/login/login.dart';
import 'package:short_drama/pages/main_tabs/main_tab.dart';

class GlobalUtil {
  static String userToken = '';
  static SharedPreferences? sharedPreferences;

  static Future<void> initGlobal() async {
    sharedPreferences = await SharedPreferences.getInstance();
    loadCache();
  }

  static void loadCache() {
    if (sharedPreferences != null) {
      userToken = sharedPreferences!.getString('userToken') ?? '';
      print('============>${userToken}');
    }
  }

  static bool hasLogin() {
    return userToken.isNotEmpty;
  }

  static void login(String token) {
    userToken = token;
    if (sharedPreferences != null) {
      sharedPreferences!.setString('userToken', userToken);
    }
    Get.off(() => MainTabPage());
  }

  static void logout() {
    userToken = '';
    if (sharedPreferences != null) {
      sharedPreferences!.remove('userToken');
    }
    Get.off(() => LoginPage());
  }
}
